package com.ys.controller.web.business;

import com.ys.entity.business.TpShopStopPhaseSet;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.Map;

@Controller
@RequestMapping("/web/business/tpShopStopPhaseSet")
public class WebTpShopStopPhaseSetController {


	/**
	 * 新增页面
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model){
		return "business/tpShopStopPhaseSet/toAdd";
	}

	/**
	 * 流程图例页面
	 */
	@GetMapping("/workFlow")
	public String workFlow(Model model){
		return "business/tpShopStopPhaseSet/workFlow";
	}


	/**
	 * 修改页面
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model,Long id,int childrenLength){

		Map entity = new TpShopStopPhaseSet().setId(id)
				.addField("U.UserId + '/' + U.UserName AS headUser ")//负责人
				.addField("U.DeptName")//负责部门
				.addField("U.DeptId")//负责部门Id
				.addField("B.NodeLft ")
				.addField("B.NodeRgt ")
				.addField("S.PhaseName AS parentName ")
				.addField("S.SetType AS parentSetType ")
				.addLeftJoin("TP_ShopStopPhaseSet S ON A.ParentId = S.Id ")
				.addLeftJoin("TS_UserInfo U ON A.HeadUserId = U.UserId ")
				.addLeftJoin("TS_OrganizationInfo B on B.Orgid = U.DeptId")
				.queryFirstMap();

		int maxPhaseNum = new TpShopStopPhaseSet().setParentId(Long.parseLong(entity.get("parentId").toString())).count();//可设置的最大阶段号
		// 获取信息
		String phaseNum = entity.get("phaseNum").toString();//阶段
		String workTime = "";
		if(entity.get("workTime")!=null){
			BigDecimal decimal = (BigDecimal)entity.get("workTime");
			workTime = decimal.intValue()+"";//时效
		}
		String headUser = entity.get("headUser")==null?"":entity.get("headUser").toString();//负责人
		String DeptName = entity.get("DeptName")==null?"":entity.get("DeptName").toString();//负责部门
		String headUserId = entity.get("headUserId")==null?"":entity.get("headUserId").toString();//负责人id
		String DeptId = entity.get("DeptId")==null?"":entity.get("DeptId").toString();//负责部门Id
		String NodeLft = entity.get("NodeLft")==null?"":entity.get("NodeLft").toString();
		String NodeRgt = entity.get("NodeRgt")==null?"":entity.get("NodeRgt").toString();
		String phaseName = (entity.get("phaseName")==null?"":entity.get("phaseName").toString());//阶段名称
		String setType = entity.get("setType")==null?"":entity.get("setType").toString();//设置类型
		String parentId = entity.get("parentId")==null?"":entity.get("parentId").toString();//父id
		String phaseType = entity.get("phaseType")==null?"":entity.get("phaseType").toString();//流程类型
		String parentNode = entity.get("parentNode")==null?"":entity.get("parentNode").toString();//所有父节点
		String parentName = entity.get("parentName")==null?"":entity.get("parentName").toString();//父节点名字
		String parentSetType = entity.get("parentSetType")==null?"":entity.get("parentSetType").toString();//父节点类型

		ToEditDto dto = new ToEditDto(id,phaseNum,workTime,headUser,DeptName,headUserId
				,DeptId,maxPhaseNum,NodeLft,NodeRgt,phaseName,setType,parentId
				,phaseType,parentNode,childrenLength,parentName,parentSetType);

		model.addAttribute("dto", dto);
		return "business/tpShopStopPhaseSet/toEdit";

	}
	@Data
	@AllArgsConstructor
	private static class ToEditDto{
		private Long id;
		private String phaseNum;//阶段
		private String workTime;//时效
		private String headUser;//负责人
		private String DeptName;//负责部门
		private String headUserId;//负责人id
		private String DeptId;//负责部门Id
		private int maxPhaseNum;
		private String NodeLft;
		private String NodeRgt;
		private String phaseName;//阶段名称
		private String setType;//设置类型
		private String parentId;//父id
		private String phaseType;//流程类型
		private String parentNode;//所有父节点
		private int childrenLength;
		private String parentName;//父节点名字
		private String parentSetType;//父节点类型
	}


}



